package com.chinaztt.fda.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chinaztt.fda.ui.R;

public final class ProgressUtil {
	private final static String TAG = new Throwable().getStackTrace()[0].getClassName();

	private static Dialog progressDialog = null;
	private static OnKeyListener mListener = null;
	//cancelFlag = false 不需要fragment控制隐藏； 为true时需要fragment控制隐藏。
	private static boolean cancelFlag = false;

	private ProgressUtil() {}

	public static void setCancelFlag(boolean flag) {
		cancelFlag = flag;
	}

	public static void show(Context context, String message) {

		if (progressDialog == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			View view = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
			LinearLayout layout = (LinearLayout) view.findViewById(R.id.dialog_view);// 加载布局

			TextView tipTextView = (TextView) view.findViewById(R.id.tipTextView);// 提示文字
			tipTextView.setText(message);// 设置加载信息

//			// main.xml中的ImageView
//			ImageView spaceshipImage = (ImageView) view.findViewById(R.id.img_loading);			
//			// 加载动画
//			Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
//					context, R.anim.loading_animation);
//			// 使用ImageView显示动画
//			spaceshipImage.startAnimation(hyperspaceJumpAnimation);

			DisplayMetrics dm = context.getResources().getDisplayMetrics();
			progressDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
			progressDialog.setCancelable(false);
			progressDialog.setCanceledOnTouchOutside(false);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(dm.widthPixels/8*7, dm.heightPixels/4);
			progressDialog.setContentView(layout, lp);// 设置布局
			progressDialog.show();
//			setOnKeyCancel();
			progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
				@Override
				public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
					if(event.getAction() == KeyEvent.ACTION_UP){
						if(KeyEvent.KEYCODE_BACK == event.getKeyCode()){
							if (mListener != null) {
								mListener.onCancel();
								return true;
							}
						}
					}
					return false;
				}
			});
			return;
		}
		if (progressDialog.isShowing()) {
			update(message);
		} else {
			update(message);
			progressDialog.show();
		}
	}

	public static void update(String message) {
		if (progressDialog == null)
			return;
		View view = progressDialog.getWindow().getDecorView();
		TextView tvUpdate = (TextView) view.findViewById(R.id.tipTextView);
		if (tvUpdate != null) {
			tvUpdate.setText(message);
		}
	}

	public static void cancel() {
		if (!cancelFlag) {
			if (progressDialog != null) {
				if (progressDialog.isShowing()) {
					progressDialog.dismiss();
				}
				progressDialog = null;
			}
			mListener = null;
		}
	}

	public static void setOnKeyListener(OnKeyListener listener){
		mListener = listener;

	}

	public interface OnKeyListener {
		public void onCancel();
	}

	//用于fragment中手动隐藏
	public static void doCancel() {
		if (progressDialog != null) {
			if (progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			cancelFlag = false;
			progressDialog = null;
		}
		mListener = null;
	}
}
